# coding: utf-8
from sqlalchemy import Column, Integer, String, JSON, DATETIME
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

# {
#   "ref": {
#     "returnFields": [
#       "id",
#       "name",
#       "type",
#       "ext",
#       "ext.desc",
#       "ext.path",
#       "ext.example",
#       "created_time"
#     ]
#   },
#   "table": "resource_api",
#   "config": [
#     {
#       "auth": {
#         "type": "annonymous",
#         "supprt": true
#       },
#       "search_delete": true,
#       "action": "create",
#       "support": true,
#       "param_check": [
#         {
#           "field": "name",
#           "min_length": 1,
#           "max_length": 100,
#           "max": 1,
#           "min": 100,
#           "not_null": true,
#           "is_null": true,
#           "regex": "",
#           "message": "",
#           "default_value": "",
#           "value": ""
#         },
#         {
#           "field": "name",
#           "min_length": 1,
#           "max_length": 100,
#           "not_null": true,
#           "message": "name长度在1-100之间"
#         },
#         {
#           "field": "ext.path",
#           "regex": "^[a-zA-Z1-9/]{1,10}$",
#           "message": "path不满足规则"
#         }
#       ],
#       "return_fields": [
#         "${ref.returnFields}"
#       ],
#       flat_ext_outer: True/False,
#       "update_fields": [
#         "name",
#         "type",
#         "ext.desc"
#       ]
#     }
#   ],
#   "id_prefix": "api"
# }

# @dataclass
class ResourceType(Base):
    __tablename__ = 'resource_type'

    id = Column(Integer, primary_key=True)
    key = Column('key', String(100))
    name = Column('name', String(100))
    ext = Column('ext', JSON())
    is_delete = Column('is_delete', Integer)
    creator = Column('creator', String(45))
    created_time = Column('created_time', DATETIME)
    updated_time = Column('updated_time', DATETIME)

    def __init__(self, id, key, name, ext, is_delete, creator, created_time,
                 updated_time):
        self.id = id
        self.key = key
        self.name = name
        self.ext = ext
        self.is_delete = is_delete
        self.creator = creator
        self.created_time = created_time
        self.updated_time = updated_time

    def __repr__(self):
        return "<ResourceType(id='%s', key='%s', name='%s', is_delete='%s', creator='%s', ext='%s', " \
               "created_time = '%s', updated_time = '%s')>" % (
                   self.id, self.key, self.name, self.is_delete, self.creator,
                   self.ext, self.created_time, self.updated_time)
